package com.seculab.admin.service.mail;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

@Service
public class MailServiceImpl implements MailService {

	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private VelocityEngine velocityEngine;
	
	@Override
	public void send(String template, final Map<String, Object> params) throws Exception {
/*		JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl)mailSender;
		mailSenderImpl.setHost("mail.seculab.co.kr");
		mailSenderImpl.setPort(110);
		mailSenderImpl.setUsername("hoonscom");
		mailSenderImpl.setPassword("wlqhol666");*/
		
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
	        public void prepare(MimeMessage mimeMessage) throws Exception {
	            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
	            message.setTo("hoonscom@gmail.com");
	            message.setFrom("hoonscom@gmail.com"); 
	            String text = VelocityEngineUtils.mergeTemplateIntoString(
	               velocityEngine, "com/seculab/admin/mail/mail.vm", "UTF-8", params);
	            message.setText(text, true);
	         }
	      };
	    this.mailSender.send(preparator);
	}
	
}
